import datetime
import logging
from os import makedirs
from os.path import join
from typing import Literal

LOG_FMT = datetime.datetime.now().strftime('%Y%m%d')
LogLevel = Literal['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL']
fmt = '%(asctime)s - %(levelname)s: %(message)s'

class Logger:
    # Map human-readable levels to logging constants
    level_relations = {
        'DEBUG': logging.DEBUG,
        'INFO': logging.INFO,
        'WARNING': logging.WARNING,
        'ERROR': logging.ERROR,
        'CRITICAL': logging.CRITICAL
    }

    def __init__(self, log_path: str, log_name: str, level: LogLevel='DEBUG', fmt: str=fmt):
        # Initialize logger configuration
        self.log_path = log_path  # Directory to store log files
        self.log_name = log_name  # Base name for log files
        self.fmt = fmt            # Log message format
        # Create named logger instance
        self.logger = logging.getLogger(log_name)
        # Set logging level
        self.logger.setLevel(self.level_relations.get(level))

    def get_logger(self) -> logging.Logger:
        """Configure and return a ready-to-use logger instance"""
        # Ensure log directory exists
        path = self.log_path
        makedirs(path, exist_ok=True)
        # Create rotating file handler
        file_name = join(path, self.log_name)
        rotate_handler = logging.FileHandler(file_name, encoding='utf-8')
        # Configure log formatting
        formatter = logging.Formatter(self.fmt)
        rotate_handler.setFormatter(formatter)
        # Attach handler to logger
        self.logger.addHandler(rotate_handler)
        return self.logger
